package io.chenglicun.modules.data.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.chenglicun.common.constant.Constant;
import io.chenglicun.common.page.PageData;
import io.chenglicun.common.service.impl.BaseServiceImpl;
import io.chenglicun.modules.data.dao.DataServiceDao;
import io.chenglicun.modules.data.entity.DataGridVillageEntity;
import io.chenglicun.modules.data.entity.DataHouseFacilitiesEntity;
import io.chenglicun.modules.data.entity.DataServiceEntity;
import io.chenglicun.modules.data.enums.DataHouseTypeEnums;
import io.chenglicun.modules.data.enums.FacilitiesAttributeEnums;
import io.chenglicun.modules.data.enums.FacilitiesTypeEnums;
import io.chenglicun.modules.data.service.DataGridVillageService;
import io.chenglicun.modules.data.service.DataServiceService;

import io.chenglicun.modules.security.user.SecurityUser;
import liquibase.pro.packaged.G;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Excel导入演示
 *
 * @author Mark sunlightcs@gmail.com
 */
@Service
public class DataServiceServiceImpl extends BaseServiceImpl<DataServiceDao, DataServiceEntity> implements DataServiceService {
    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private DataGridVillageService dataGridVillageService;

    @Override
    public boolean excelData(List<List<Object>> list) {
        List<DataServiceEntity> insertList = new ArrayList<>();
        for (List<Object> o : list) {
            try {
                DataServiceEntity entity = new DataServiceEntity();
                if (o.size() > 1) {
                    String houseType = o.get(0) == null ? null : o.get(0).toString();//house_type//第一行房屋类型
                    if (houseType != null) {
                        entity.setHouseType(houseType);
                    }
                }
                if (o.size() > 2) {
                    String serviceWindow = o.get(1) == null ? null : o.get(1).toString();//facilities_attribute//第二行服务窗口
                    entity.setServiceWindow(serviceWindow);
                }

                if (o.size() > 3) {
                    String functionalRoom = o.get(2) == null ? null : o.get(2).toString();//facilities_type//第三行功能室
                    entity.setFunctionalRoom(functionalRoom);
                }
                if (o.size() > 4) {
                    String transactionApplication = o.get(3) == null ? null : o.get(3).toString();//facilities_type//第四行网上事务申请
                    entity.setTransactionApplication(transactionApplication);
                }
                if (o.size() > 5) {
                    String governmentPlatformLink = o.get(4) == null ? null : o.get(4).toString();//facilities_head//第五行政务平台链接
                    entity.setGovernmentPlatformLink(governmentPlatformLink);
                }

                if (o.size() > 6) {
                    String serviceImg = o.get(5) == null ? null : o.get(5).toString();//facilities_head_phone//第六行图片
                    entity.setServiceImg(serviceImg);
                }

                if (o.size() > 7) {
                    String facilitiesHead = o.get(6) == null ? null : o.get(6).toString();//facilities_use//第七行设施负责人（维护人）
                    entity.setFacilitiesHead(facilitiesHead);
                }

                if (o.size() > 8) {
                    String facilitiesHeadPhone = o.get(7) == null ? null : o.get(7).toString();//facilities_img//第八行负责人（维护人联系电话）
                    entity.setFacilitiesHeadPhone(facilitiesHeadPhone);
                }

                if (o.size() > 9) {
                    String mapCoordinates = o.get(8) == null ? null : o.get(8).toString();//facilities_information//第九行地图坐标
                    entity.setMapCoordinates(mapCoordinates);
                }
                entity.setCreator(SecurityUser.getUserId());
                entity.setCreateDate(new Date());
                entity.setUpdater(SecurityUser.getUserId());
                entity.setUpdaterDate(new Date());
                insertList.add(entity);
            } catch (Exception e) {
                logger.info("插入失败：" + JSONUtil.toJsonStr(o));
            }

        }
        super.insertBatch(insertList);
        return true;
    }


    public PageData<DataServiceEntity> page(Map<String, Object> params) {
        IPage<DataServiceEntity> page = baseDao.selectPage(
                getPage(params, Constant.CREATE_DATE, false),
                getWrapper(params)
        );
        return getPageData(page, DataServiceEntity.class);
    }





    public void update(DataServiceEntity entity) {
        entity.setUpdater(SecurityUser.getUserId());
        entity.setUpdaterDate(new Date());
        super.updateById(entity);
    }

    public void delete(Integer[] ids) {
        super.deleteBatchIds(Arrays.asList(ids));
    }
    public void save(DataServiceEntity entity) {
        entity.setCreator(SecurityUser.getUserId());
        entity.setCreateDate(new Date());
        super.insert(entity);
    }
    public QueryWrapper<DataServiceEntity> getWrapper(Map<String, Object> params) {
        QueryWrapper<DataServiceEntity> wrapper = new QueryWrapper<>();

        String houseType = (String) params.get("houseType");
        wrapper.eq(StringUtils.isNotBlank(houseType), "house_type", houseType);
        wrapper.orderByDesc("create_date");
        return wrapper;
    }
}